package mapeo;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import entidades.Extension;

public class MapExtension implements IBaseDatos{
	
	private Session session;
	private Transaction transaction;
	private Extension extension;
	
	public MapExtension(){
		session = null;
		extension = null;
	}
	
	public Object save(Object objGuardar){
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = null;
		try{
			extension = (Extension) objGuardar;
		}
		catch(ClassCastException e){
			System.out.println("ERROR: No se puede castear el objeto a Extension");
			return null;
		}
		String extensionId = null;
		
		try {
			transaction = session.beginTransaction();
			extensionId = (String) ( session.save(extension) );
			transaction.commit();
		} catch (HibernateException e) {
			System.out.println("ERROR: Hibernate no puede realizar el grabado de la Extension");
			transaction.rollback();
			return null;
		} 
		finally {
			session.close();
		}
		return extensionId;
	}
	
	public boolean update(Object actualizado){
		Extension aux;
		try{
			aux= (Extension) actualizado;
		}catch (ClassCastException e){
			System.out.println("ERROR: Error en el casteo de parametros");
			return false;
		}		
		boolean flag = true;
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = null; 
		try {
			transaction = session.beginTransaction();
			extension = aux;
			session.update(extension);
			transaction.commit();
			} 
		catch (HibernateException e) {
			transaction.rollback();
			System.out.println("ERROR: Hibernate no puede realizar la actualizacion de la Extension");
			flag = false;
		} 
		finally {
			session.close();
		}
		return flag;
	}
	
	public boolean delete(Object primaryKey){
		String key;
		try{
			key= (String) primaryKey;
		}catch(ClassCastException e){
			System.out.println("ERROR: No se puede castear el objeto a String");
			return false;
		}
		boolean flag= true; 
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = null;
		try {
			transaction = session.beginTransaction();
			extension = (Extension) session.get( Extension.class, key );
			if (extension == null){
				return false;
			}
			session.delete(extension);
			transaction.commit();
		} 
		catch (HibernateException e) {
			transaction.rollback();
			System.out.println("ERROR: Hibernate no puede realizar el borrado de la Extension");
			flag = false;
			} 
		finally {
			session.close();
			}
		return flag;
	}

	@Override
	public Object get(Object primaryKey) {
		String key;
		try{
			key = (String) primaryKey;
		}catch(ClassCastException e){
			System.out.println("ERROR: No se puede castear el objeto a String");
			return null;
		}
		extension = null;
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = null;
		try {
			transaction = session.beginTransaction();
			extension = (Extension) session.get( Extension.class, key );
			} 
		catch (	HibernateException e) {
			System.out.println("ERROR: Hibernate no puede obtener la Extension");
			return false;
		} 
		finally {
			session.close();
		}
		return extension;
	}

	/**
	 * Recorre la base de datos en busca de todas las extensiones que tiene grabadas
	 * @return una lista con todas las extensiones del sistema.
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Extension> obtenerExtensionesSistema(){
		ArrayList<Extension> extensiones = new ArrayList<Extension>();
		try {
			 session = HibernateUtil.getSessionFactory().openSession();
			 extensiones = (ArrayList<Extension>) session.createCriteria(Extension.class).addOrder(Order.asc("clave")).list();
			} 
		catch (	HibernateException e) {
			System.out.println("ERROR: Hibernate no puede obtener la Extension del sistema");
			return null;
		} 
		finally {
			session.close();
			}
		return extensiones;
	}

	@Override
	public boolean deleteAll() {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = null;
		try{
			String hql = "delete from Extension";
			Query query = session.createQuery(hql);
			transaction = session.beginTransaction();
			query.executeUpdate();
			transaction.commit();
		}catch ( HibernateException e){
			System.out.println("ERROR: Hibernate no puede borrar todos las Extensiones");
			transaction.rollback();
			return false;
		}
		finally{
			session.close();
		}
		return true;
	}
}
